package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.AdjustInOutStockOrder;
import com.turing.post.cardmanagement.util.AdjustInOutStockOrderDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 调整单管理
 * 
 * @author YangShuo
 * 
 */
@Stateless(mappedName = "cardmanagement.impl.AdjustInOutStockOrderDaoImpl")
@Remote
@Local
public class AdjustInOutStockOrderDaoImpl implements AdjustInOutStockOrderDao {

	@PersistenceContext
	protected EntityManager _em;

	@Override
	public AdjustInOutStockOrder getAdjustInOutStockOrderByAdjustInOutStockOrderId(
			BigInteger AdjustInOutStockOrderId) {
		String sql="select * from AdjustInOutStockOrder where AdjustInOutStockOrderId=?1 or OrderNumber=?1";
		Query query=_em.createNativeQuery(sql, AdjustInOutStockOrder.class);
		query.setParameter(1, AdjustInOutStockOrderId);
		return (AdjustInOutStockOrder)query.getSingleResult();
	}

	@Override
	public void insertAdjustInOutStockOrder(
			AdjustInOutStockOrder AdjustInOutStockOrd)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, AdjustInOutStockOrd);
	}

	@Override
	public void updateAdjustInOutStockOrder(
			AdjustInOutStockOrder AdjustInOutStockOrder)
			throws PCPSApplicationException {
		GeneralDao.update(_em, AdjustInOutStockOrder);
	}

	@Override
	public int updateAdjustInOutStockOrderCancelID(
			BigInteger AdjustInOutStockOrderID,
			BigInteger CancelAdjustInOutStockOrderID)
			throws PCPSApplicationException {
		String sql = "update AdjustInOutStockOrder set CancelAdjustInOutStockOrderId="
				+ CancelAdjustInOutStockOrderID
				+ " where AdjustInOutStockOrderID=" + AdjustInOutStockOrderID;
		Query query = _em.createNativeQuery(sql);
		return query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AdjustInOutStockOrder> getAdjustInOutStockOrderList(
			Integer flag, String Branchno, String StartTime, String EndTime,
			String TranType, String Status, BigInteger OperId,
			BigInteger AuthOperId, Integer page) {
		String sql = "select * from AdjustInOutStockOrder t where 1=1 "
				+ "and (-1=?1 or (nvl(t.issuebranchno,'')||nvl(t.salebranchno,''))=?2) "
				+ "and (-1<>?1 or (nvl(t.issuebranchno,'')||nvl(t.salebranchno,'')) in (select subbranchno from SYSBRANCHSUBORD where branchno=?2)) "
				+ "and (?3 is null or trantime>=to_date(?3,'yyyy-mm-dd hh24:mi:ss')) "
				+ "and (?4 is null or trantime<=to_date(?4,'yyyy-mm-dd hh24:mi:ss')) "
				+ "and ('-1'=?5 or t.trantype=?5) "
				+ "and (-1=?6 or t.operid=?6) "
				+ "and (-1=?7 or t.authoperid=?7) "
				+ "and ('-1'=?8 or t.status=?8)" + "order by t.trantime desc";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, AdjustInOutStockOrder.class);
		query.setParameter(1, flag);
		query.setParameter(2, Branchno);
		query.setParameter(3, StartTime + " 00:00:00");
		query.setParameter(4, EndTime + " 23:59:59");
		query.setParameter(5, TranType);
		query.setParameter(6, OperId);
		query.setParameter(7, AuthOperId);
		query.setParameter(8, Status);
		if(page>=0){			
		   query.setFirstResult(startPosition);
		   query.setMaxResults(maxResult);
		}
		return query.getResultList();
	}

	@Override
	public Integer getAdjustInOutStockOrderCount(Integer flag, String Branchno,
			String StartTime, String EndTime, String TranType, String Status,
			BigInteger OperId, BigInteger AuthOperId) {
		String sql = "select nvl(count(*),0) from AdjustInOutStockOrder t where 1=1 "
				+ "and (-1=?1 or (nvl(t.issuebranchno,'')||nvl(t.salebranchno,''))=?2) "
				+ "and (-1<>?1 or (nvl(t.issuebranchno,'')||nvl(t.salebranchno,'')) in (select subbranchno from SYSBRANCHSUBORD where branchno=?2)) "
				+ "and (?3 is null or trantime>=to_date(?3,'yyyy-mm-dd hh24:mi:ss')) "
				+ "and (?4 is null or trantime<=to_date(?4,'yyyy-mm-dd hh24:mi:ss')) "
				+ "and ('-1'=?5 or t.trantype=?5) "
				+ "and (-1=?6 or t.operid=?6) "
				+ "and (-1=?7 or t.authoperid=?7) "
				+ "and ('-1'=?8 or t.status=?8)" + "order by t.trantime desc";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, flag);
		query.setParameter(2, Branchno);
		if (PCPSUtil.isEmpty(StartTime)) {
			query.setParameter(3, "");
		} else {
			query.setParameter(3, StartTime + " 00:00:00");
		}
		if (PCPSUtil.isEmpty(EndTime)) {
			query.setParameter(4, "");
		} else {
			query.setParameter(4, EndTime + " 23:59:59");
		}
		query.setParameter(5, TranType);
		query.setParameter(6, OperId);
		query.setParameter(7, AuthOperId);
		query.setParameter(8, Status);
		return Integer.parseInt(query.getSingleResult().toString());
	}

	@Override
	public void deleteAdjustInOutStockOrder(BigInteger adjustInOutStockOrderId)
			throws PCPSApplicationException {
		AdjustInOutStockOrder aioso = getAdjustInOutStockOrderByAdjustInOutStockOrderId(adjustInOutStockOrderId);
		if (aioso != null) {
			GeneralDao.remove(_em, aioso);
		}
	}
}
